﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Collections.ObjectModel;

namespace PurchaseManager
{
    /// <summary>
    /// Interaction logic for InsertCompany.xaml
    /// </summary>
    public partial class InsertCompany : UserControl
    {
        MainWindow _mainWnd = Application.Current.MainWindow as MainWindow;

        ObservableCollection<Company> _companyList = new ObservableCollection<Company>();

        public InsertCompany()
        {
            InitializeComponent();

            _dgCompany.DataContext = _companyList;

            Refresh();
        }

        public void Refresh()
        {
            _companyList.Clear();

            foreach (Company cItem in _mainWnd.GetCompanyList())
            {
                _companyList.Add(cItem);
            }
        }

        private void _dgCompany_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            e.Row.Background = Brushes.IndianRed;
        }

        private void _btnDelete_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
        }

        private void _btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            ApplyDB();
        }

        private void _btnInit_Click(object sender, RoutedEventArgs e)
        {
            // 다시 가져오기
            Refresh();
        }

        private void _btnAdd_Click(object sender, RoutedEventArgs e)
        {
            foreach (Company cItem in _companyList)
            {
                if (string.IsNullOrEmpty(cItem.Name)) return;
            }

            Company newItem = new Company();
            _companyList.Add(newItem);

            _dgCompany.ScrollIntoView(newItem);
            _dgCompany.SelectedItem = newItem;
        }

        private void DeleteItems()
        {
            try
            {
                List<string> nameList = new List<string>();

                foreach (Company cItem in _dgCompany.SelectedItems)
                {
                    nameList.Add(cItem.Name);
                }

                foreach (string name in nameList)
                {
                    for (int i = _companyList.Count - 1; i >= 0; i--)
                    {
                        if (name == _companyList[i].Name)
                        {
                            _companyList.RemoveAt(i);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.Log(ex.ToString());
            }
        }

        public void ApplyDB()
        {
            string msg = "현재 데이터를 DB에 적용하시겠습니까?";

            MessageBoxResult result = MessageBox.Show(msg, "Waring", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No, MessageBoxOptions.DefaultDesktopOnly);

            if (result == MessageBoxResult.Yes)
            {
                if (_companyList != null)
                {
                    _mainWnd.UpdateCompanyList(_companyList);
                }
            }
        }
    }
}
